#pragma once

#import "msxml4.dll"
#include <atlbase.h>
#include <memory>

#ifndef RELEASE_PTR
#define RELEASE_PTR(x) \
    if( x != NULL )  \
 {     \
 x.Release(); \
 x = NULL;  \
 }
#endif

class CXml;
class CXmlNode;
class CXmlNodes;

typedef std::auto_ptr<CXmlNode>  CXmlNodePtr;
typedef std::auto_ptr<CXmlNodes>  CXmlNodesPtr;

//////////////////////////////////////////////////////////////////////////

#include ".\XmlNodes.h"

class CXmlNode
{
    friend class CXml;
    friend class CXmlNode;
    friend class CXmlNodes;
    
protected:
    
    MSXML2::IXMLDOMNodePtr  m_pNode;
    
    CXmlNode( MSXML2::IXMLDOMNodePtr pNode);
    
    BOOL _GetValue(CString & strValue);
    BOOL _SetValue(CString & strValue);
    
    BOOL _GetAttribute( CString & strName, CString & strValue);
    BOOL _SetAttribute( CString & strName, CString & strValue);
    
public:
    
    CXmlNode(void);
    CXmlNode( CXmlNode & refNode IN);
    ~CXmlNode(void);
    
    
    CXmlNodePtr operator = ( CXmlNodePtr pNode);
    CXmlNode & operator = ( CXmlNode & refNode);
    
    
    BOOL IsNull(void);   
    CString GetName(void);  
    CXmlNode & Remove(void); 
    void Release(void);
    
    
    CXmlNodePtr GetChild( CString strName, BOOL bBuildIfNotExist = TRUE);
    CXmlNodePtr NewChild( CString strName );
    CXmlNodePtr GetParent(void);
    CXmlNodesPtr GetChildren();
    void AddChild( CXmlNodePtr & pChildNode);
    void AddChild( CXmlNode & refChildNode);
    BOOL HasChildren(void);
    BOOL RemoveChildren(void);
    
    CString GetAttribute( CString strName, LPCTSTR lpszDefault = NULL);
    bool GetAttribute( CString strName, bool bDefault); 
    int  GetAttribute( CString strName, int nDefault); 
    float GetAttribute( CString strName, float fDefault); 
    double GetAttribute( CString strName, double dDefault);
    
    BOOL SetAttribute( CString strName, LPCTSTR lpszValue);
    BOOL SetAttribute( CString strName, bool bValue);
    BOOL SetAttribute( CString strName, int nValue);
    BOOL SetAttribute( CString strName, float fValue);
    BOOL SetAttribute( CString strName, double dValue);
    
    BOOL RemoveAttribute( CString strName );
    
    CString GetValue( LPCTSTR lpszDefault = NULL );
    bool GetValue( bool bDefault );
    int  GetValue( int nDefault);
    float GetValue( float fDefault);
    double GetValue( double dDefault);
    
    BOOL SetValue( LPCTSTR lpszValue );
    BOOL SetValue( bool bValue );
    BOOL SetValue( int nValue );
    BOOL SetValue( float fValue );
    BOOL SetValue( double dValue );
    
    
    CXmlNodePtr SelectSingleNode(LPCTSTR lpszPath);
    CXmlNodesPtr SelectNodes(LPCTSTR lpszPath);
    
};
